package com.robam.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.robam.gulimall.product.entity.CategoryEntity;
import com.robam.gulimall.product.service.CategoryService;
import com.robam.common.utils.PageUtils;
import com.robam.common.utils.R;



/**
 * 商品三级分类
 *
 * @author bobo
 * @email 412467850@qq.com
 * @date 2020-10-15 08:42:51
 */
@Api(description = "商品三级分类")
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "查询三级分类树形结构")
    @GetMapping("/tree")
    public R tree() {
        List<CategoryEntity> tree = categoryService.getTree();
        return R.ok().put("tree",tree);
    }

    /**
     * 列表
     */
    @ApiOperation(value = "分页查询三级分类")
    @GetMapping("/list")
    //@RequiresPermissions("product:category:list")
    public R list(@ApiParam(name = "params", value = "请求参数") @RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @ApiOperation(value = "根据id查询分类信息")
    @GetMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@ApiParam(name = "catId", value = "分类id") @PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增分类信息")
    @PostMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@ApiParam(name = "category", value = "分类对象") @RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "单笔修改分类信息")
    @PostMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@ApiParam(name = "category", value = "分类对象") @RequestBody CategoryEntity category){
		categoryService.updateById(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "根据id列表批量删除分类")
    @PostMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@ApiParam(name = "catIds", value = "id列表") @RequestBody Long[] catIds){
//		categoryService.removeByIds(Arrays.asList(catIds));
        categoryService.removeCategoryByIds(Arrays.asList(catIds));
        return R.ok();
    }

    /**
     * 批量修改
     */
    @ApiOperation(value = "批量修改分类信息")
    @PostMapping("/batchUpdate")
    //@RequiresPermissions("product:category:update")
    public R batchUpdate(@ApiParam(name = "categoryEntityList", value = "分类对象列表") @RequestBody List<CategoryEntity> categoryEntityList){
        categoryService.updateBatchById(categoryEntityList);
        return R.ok();
    }

}
